package com.hotel.BL;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.EntityName;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.datastore.Query.CompositeFilterOperator;
import com.google.appengine.api.datastore.Query.Filter;
import com.google.appengine.api.datastore.Query.FilterOperator;
import com.google.appengine.api.datastore.Query.FilterPredicate;

import data.POS;
import data.POSKitchen;

public class POSKitchenLogic extends AbstractBL {
	public POSKitchenLogic(HttpServletRequest req,HttpServletResponse resp){
		this.req = req;
		this.resp = resp;
	}
	
	public List<POSKitchen> GeneratePOSKitchen(long hotelId)
	{
		List<POSKitchen> list = new ArrayList<POSKitchen>();
		POSLogic posLogic = new POSLogic(req,resp);
		
		POS iChic = posLogic.GetPOS(hotelId, "iChic");
		POS iCoffee = posLogic.GetPOS(hotelId, "iCoffee");
		
		POSKitchen k;
		if(GetPOSKitchen(hotelId,"iChic Kitchen") == null){
			k = new POSKitchen();
			k.setHotelId(hotelId);
			k.setTitle("iChic Kitchen");
			k.setCode("iChic Kitchen");
			k.setPOSId(iChic.getId());
			list.add(k);
		}
		if(GetPOSKitchen(hotelId,"iBar") == null){
			k = new POSKitchen();
			k.setHotelId(hotelId);
			k.setTitle("iBar");
			k.setCode("iBar");
			k.setPOSId(iChic.getId());
			list.add(k);
		}if(GetPOSKitchen(hotelId,"iCoffee Table") == null){
			k = new POSKitchen();
			k.setHotelId(hotelId);
			k.setTitle("iCoffee Table");
			k.setCode("iCoffee Table");
			k.setPOSId(iCoffee.getId());
			list.add(k);
		}
		
		return list;
	}
	
	public POSKitchen GetPOSKitchen(long id)
	{
		Key key = KeyFactory.createKey(EntityName.POSKitchen, id);
		try {
			Entity lang = datastore.get(key);
			POSKitchen c = new POSKitchen();
			c.setEntity(lang);
			return c;
		} catch (EntityNotFoundException e) {
			return null;
		}
	}
	
	public POSKitchen GetPOSKitchen(Entity entity)
	{
		if(entity == null) return null;
		POSKitchen item = new POSKitchen();
		item.setEntity(entity);
		return item;
	}
	
	public POSKitchen GetPOSKitchen(long hotelId,String title)
	{
		Query q = new Query(EntityName.POSKitchen);
		
		Filter filterHotel = new FilterPredicate("hotelId",FilterOperator.EQUAL,hotelId);
		Filter filterStatus = new FilterPredicate("status",FilterOperator.EQUAL,true);
		Filter filterAvaliable = new FilterPredicate("avaliable",FilterOperator.EQUAL,true);
		Filter filterTitle = new FilterPredicate("title",FilterOperator.EQUAL,title);

		Filter filter = CompositeFilterOperator.and(filterHotel, filterStatus, filterAvaliable,filterTitle);
		
		q.setFilter(filter);
		
		return GetPOSKitchen(datastore.prepare(q).asSingleEntity());
	}
	
	public List<POSKitchen> GetPOSKitchenList(List<Entity> entities)
	{
		List<POSKitchen> list = new ArrayList<POSKitchen>();
		for(Entity e : entities)
		{
			POSKitchen a = new POSKitchen();
			a.setEntity(e);
			list.add(a);
		}
		return list;
	}
}
